package com.android.ailaoshan.user;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录过程处理
 */
public class LoginHandler {
    private SharedPreferences mSharedPreferences;

    public LoginHandler(Context mContext) {
        mSharedPreferences = mContext.getSharedPreferences("userLogin",Context.MODE_PRIVATE);
    }

    /**
     * 添加数据
     */
    public void addData(String userName,String userPswd,int userId){
        SharedPreferences.Editor editor = mSharedPreferences.edit();

        editor.putString("userName",userName);
        editor.putString("userPswd",userPswd);
        editor.putInt("userID",userId);

        editor.apply();
    }

    /**
     * 读取数据
     */
    public Map<String, Object> getData(){
        Map<String,Object> userMap = new HashMap<>();

        userMap.put("userName",mSharedPreferences.getString("userName",""));
        userMap.put("userPswd",mSharedPreferences.getString("userPswd",""));
        userMap.put("userID",mSharedPreferences.getInt("userID",0));

        return userMap;
    }

    /**
     * 判断是否登录
     */
    public boolean isLogin(){
        Map<String,Object> userMap = getData();

        String userId = userMap.get("userID").toString();

        return (!userId.equals("0"));
    }

}
